package vip.xiaonuo.smzq.modular.psba.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.net.URLDecoder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.smzq.core.tools.file.PathUtils;
import vip.xiaonuo.smzq.modular.psba.entity.GtWjglb;
import vip.xiaonuo.smzq.modular.psba.mapper.GtWjglbMapper;
import vip.xiaonuo.smzq.modular.psba.service.IGtWjglbService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.smzq.modular.user.entity.User;
import vip.xiaonuo.smzq.modular.user.mapper.UserMapper;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 评审备案-文件关联表 服务实现类
 *
 * @author sqq
 * @since 2024-06-11
 */
@Service
public class GtWjglbServiceImpl extends ServiceImpl<GtWjglbMapper, GtWjglb> implements IGtWjglbService {

    @Resource
    private GtWjglbMapper gtWjglbMapper;
    @Resource
    private UserMapper userMapper;

    @Value("${file.base}")
    String baseFilePath;


    @Override
    public Page<GtWjglb> page(GtWjglb gtWjglb) {
        QueryWrapper<GtWjglb> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public String fileUpload(MultipartFile file) {
        // 获取用户信息
        SaBaseLoginUser saBaseLoginUser = StpLoginUserUtil.getLoginUser();
        User user = userMapper.selectById(saBaseLoginUser.getId());
            GtWjglb gtWjglb=new GtWjglb();
            //文件名称

            String fileName = file.getOriginalFilename();
            File file1 = new File(PathUtils.getUpLoadPath(baseFilePath) + File.separator  + fileName);
            try {
                FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            fileName = URLDecoder.decode(fileName, StandardCharsets.UTF_8);//解码
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String wjName = fileName.substring(0, fileName.lastIndexOf("."));
            //设置文件清单主键id
            String uuidfile = UUID.randomUUID().toString();
            gtWjglb.setPkid(uuidfile);//id
            gtWjglb.setWjmc(wjName);//文件名称
            gtWjglb.setWjlj(file1.getPath());//文件路径
            gtWjglb.setWjhz(suffix);//文件后缀
            gtWjglb.setWjdx(String.valueOf(file.getSize()));//文件大小
            gtWjglb.setScsj(DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss")); //上传时间
            gtWjglb.setScr(user.getId());//上传人
            gtWjglb.setScrmc(user.getName());//上传人名称
            gtWjglbMapper.insert(gtWjglb);

        return uuidfile;
    }

    @Override
    public List<GtWjglb> gtWjglbList(String fileId) {
        List<String> fileIdList = Arrays.stream(fileId.split(","))
                .collect(Collectors.toList());
        QueryWrapper<GtWjglb> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("pkid",fileIdList);
        return gtWjglbMapper.selectList(queryWrapper);
    }
}
